package common

import (
	"github.com/astaxie/beego"
	"gopkg.in/resty.v0"
)

var parse_server_path = beego.AppConfig.String("parse_server_path")
var parse_app_id = beego.AppConfig.String("parse_appid")
var parse_master_key = beego.AppConfig.String("parse_master_key")

func H5_parse_get(object_name string, class_name string, id string, appID string, masterKey string) (respBody string, err error) {
	_url := getServerPort(masterKey) + "/" + object_name
	if class_name != "" {
		_url += "/" + class_name
		if id != "" {
			_url += "/" + id
		}
	}
	_resp, err := resty.R().SetHeaders(map[string]string{
		"Content-Type":           "application/json",
		"X-Parse-Application-Id": appID,
		"X-Parse-Master-Key":     masterKey,
	}).Get(_url)

	respBody = _resp.String()
	return
}

func H5_parse_get_by(object_name string, class_name string, field string, field_s string, appID string, masterKey string, types string) (respBody string, err error) {
	_url := getServerPort(masterKey) + "/" + object_name + "/" + class_name
	var _data string
	var _resp *resty.Response

	if types == "all" {
		if field == "" {
			_data = `where={"` + field + `":"` + field_s + `"}`
		} else {
			_data = `where={"` + field + `":{"$regex":"^` + field_s + `"}}`
		}

	} else {
		_data = `where={"` + field + `":"` + field_s + `"}`
	}
	beego.Debug(_data)
	_resp, err = resty.R().SetQueryString(_data).SetHeaders(map[string]string{
		"Content-Type":           "application/json",
		"X-Parse-Application-Id": appID,
		"X-Parse-Master-Key":     masterKey,
	}).Get(_url)
	beego.Debug(_resp)
	respBody = _resp.String()
	return
}

func H5_parse_post(object_name string, class_name string, data interface{}, appID string, masterKey string) (respBody string, err error) {
	_url := getServerPort(masterKey) + "/" + object_name + "/" + class_name
	_resp, err := resty.R().SetBody(data).SetHeaders(map[string]string{
		"Content-Type":           "application/json",
		"X-Parse-Application-Id": appID,
		"X-Parse-Master-Key":     masterKey,
	}).Post(_url)

	respBody = _resp.String()
	return
}

func H5_parse_put(object_name string, class_name string, id string, data interface{}, appID string, masterKey string) (respBody string, err error) {

	_url := getServerPort(masterKey) + "/" + object_name
	if class_name != "" {
		_url += "/" + class_name
	}
	if id != "" {
		_url += "/" + id
	}
	_resp, err := resty.R().SetBody(data).SetHeaders(map[string]string{
		"Content-Type":           "application/json",
		"X-Parse-Application-Id": appID,
		"X-Parse-Master-Key":     masterKey,
	}).Put(_url)

	respBody = _resp.String()
	return
}

func H5_parse_delete(object_name string, class_name string, id string, appID string, masterKey string) (respBody string, err error) {
	_url := getServerPort(masterKey) + "/" + object_name
	if class_name != "" {
		_url += "/" + class_name
	}
	if id != "" {
		_url += "/" + id
	}
	beego.Debug(_url)
	_resp, err := resty.R().SetHeaders(map[string]string{
		"Content-Type":           "application/json",
		"X-Parse-Application-Id": appID,
		"X-Parse-Master-Key":     masterKey,
	}).Delete(_url)

	respBody = _resp.String()
	return
}

func getServerPort(masterKey string) string {
	switch masterKey {
	case "1VemFayozIlpU4iEmofzSEvf1467078871":
		return "http://54.223.109.139:1337/parse"
	case "kjLzuM6BripEGyPsjZc3zZs71468569398":
		return "http://54.223.109.139:8000/parse"
	case "x5vwHyeGKkhshiqfU4VwRC8d1471360701":
		return "http://54.223.109.139:8002/parse"
	default:
		return "http://54.223.109.139:1337/parse"
	}
}
